/**
 * @File Name:   FixedRollingFileAppender.java
 * @Description: 
 * @Create By :  weixink
 */
package com.cgw.common.log;

/**
 * Title:FixedRollingFileAppender<br>
 * Description:<br>
 * Copyright (c) 2008<br>
 * 
 * @author weixink<br>
 * @version 1.0<br>
 */
import java.io.File;
import java.io.IOException;

import org.apache.log4j.Layout;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.RollingFileAppender;
/**
 * @author: weixink
 * @since: FixedRollingFileAppender.java
 */

public class TimeSizeRollingFileAppender extends RollingFileAppender
{
    private final static String FILE_SEP = System.getProperty("file.separator");

    protected int logInServer = 0;
    static final int NOT_LOG_IN_SERVER = 0;
    static final int LOG_IN_SERVER = 1;

//    SimpleDateFormat sdf;

    public TimeSizeRollingFileAppender()
    {
        super();
    }

    public TimeSizeRollingFileAppender(Layout layout, String filename,boolean append) throws IOException
    {
        super(layout, filename, append);
    }
    
    public void setFile(String file)
    {
        // Trim spaces from both ends. The users probably does not want
        // trailing spaces in file names.
        String val = file.trim();
        String tmpfileName = val.replace('/', FILE_SEP.charAt(0));
        /*
         * if (!tmpfileName.startsWith(LogConstants.FILE_SEP)) {
         * tmpfileName=LogConstants.FILE_SEP+tmpfileName; }
         */
        if (tmpfileName != null)
        {
            if (tmpfileName.startsWith("/"))
            {
                logInServer = NOT_LOG_IN_SERVER;
            } else if (tmpfileName.indexOf(":") != -1)
            {
                logInServer = NOT_LOG_IN_SERVER;
            }
        }
        if (logInServer == LOG_IN_SERVER)
        {
            String weblogicPath = System.getProperty("user.dir");
            System.out.println(weblogicPath);
            tmpfileName = weblogicPath + FILE_SEP + tmpfileName;
            System.out.println(tmpfileName);
        }
//        sdf = new SimpleDateFormat("yyyy-MM-dd");
//        int a = tmpfileName.lastIndexOf(FILE_SEP);
//        if (a > 0)
//        {
//            String temp = tmpfileName.substring(a + 1, tmpfileName.length());
//            String qianStr = tmpfileName.substring(0, a + 1); // ǰ׺
//            int i = temp.lastIndexOf(".");
//            if (i > 0)
//            {
//                String temp1 = temp.substring(0, i);
//                String houStr = temp.substring(i, temp.length());
//                tmpfileName = qianStr + temp1 + "_"
//                        + sdf.format(new Date(System.currentTimeMillis()))
//                        + houStr;
//            }
//        }

        fileName = tmpfileName;
        // create non-exist path
        LogLog.debug("fileName:" + tmpfileName);

        int index = fileName.lastIndexOf(FILE_SEP);
        if (index > 0)
        {
            String sPath = fileName.substring(0, index);
            File path = new File(sPath);
            if (!path.exists())
            {
                path.mkdirs();
            }
        }

        LogLog.debug("File set:" + fileName);
    }
}
